home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / shllutil.lha / shellutils-1.8 / src / env.c < prev    next >
C/C++ Source or Header  |  1991-12-07  |  5KB  |  169 lines

  1. /* env - run a program in a modified environment
  2.    Copyright (C) 1986, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Richard Mlynarik and David MacKenzie */
  19.  
  20. /* Options:
  21.    -
  22.    -i
  23.    --ignore-environment
  24.     Construct a new environment from scratch; normally the
  25.     environment is inherited from the parent process, except as
  26.     modified by other options.
  27.  
  28.    -u variable
  29.    --unset=variable
  30.     Unset variable VARIABLE (remove it from the environment).
  31.     If VARIABLE was not set, does nothing.
  32.  
  33.    variable=value (an arg containing a "=" character)
  34.     Set the environment variable VARIABLE to value VALUE.  VALUE
  35.     may be of zero length ("variable=").  Setting a variable to a
  36.     zero-length value is different from unsetting it.
  37.  
  38.    --
  39.     Indicate that the following argument is the program
  40.     to invoke.  This is necessary when the program's name
  41.     begins with "-" or contains a "=".
  42.  
  43.    The first remaining argument specifies a program to invoke;
  44.    it is searched for according to the specification of the PATH
  45.    environment variable.  Any arguments following that are
  46.    passed as arguments to that program.
  47.  
  48.    If no command name is specified following the environment
  49.    specifications, the resulting environment is printed.
  50.    This is like specifying a command name of "printenv".
  51.  
  52.    Examples:
  53.  
  54.    If the environment passed to "env" is
  55.     { LOGNAME=rms EDITOR=emacs PATH=.:/gnubin:/hacks }
  56.  
  57.    env - foo
  58.     runs "foo" in a null environment.
  59.  
  60.    env foo
  61.     runs "foo" in the environment
  62.     { LOGNAME=rms EDITOR=emacs PATH=.:/gnubin:/hacks }
  63.  
  64.    env DISPLAY=gnu:0 nemacs
  65.     runs "nemacs" in the envionment
  66.     { LOGNAME=rms EDITOR=emacs PATH=.:/gnubin:/hacks DISPLAY=gnu:0 }
  67.  
  68.    env - LOGNAME=foo /hacks/hack bar baz
  69.     runs the "hack" program on arguments "bar" and "baz" in an
  70.     environment in which the only variable is "LOGNAME".  Note that
  71.     the "-" option clears out the PATH variable, so one should be
  72.     careful to specify in which directory to find the program to
  73.     call.
  74.  
  75.    env -u EDITOR LOGNAME=foo PATH=/energy -- e=mc2 bar baz
  76.     runs the program "/energy/e=mc2" with environment
  77.     { LOGNAME=foo PATH=/energy }
  78. */
  79.  
  80. #include <stdio.h>
  81. #include <getopt.h>
  82. #include <sys/types.h>
  83. #include "system.h"
  84.  
  85. int putenv ();
  86. void error ();
  87. void usage ();
  88.  
  89. extern char **environ;
  90.  
  91. /* The name by which this program was run. */
  92. char *program_name;
  93.  
  94. struct option longopts[] =
  95. {
  96.   {"ignore-environment", 0, NULL, 'i'},
  97.   {"unset", 1, NULL, 'u'},
  98.   {NULL, 0, NULL, 0}
  99. };
  100.  
  101. void
  102. main (argc, argv, envp)
  103.      register int argc;
  104.      register char **argv;
  105.      char **envp;
  106. {
  107.   char *dummy_environ[1];
  108.   int optc;
  109.   int ignore_environment = 0;
  110.  
  111.   program_name = argv[0];
  112.  
  113.   while ((optc = getopt_long (argc, argv, "+iu:", longopts, (int *) 0)) != EOF)
  114.     {
  115.       switch (optc)
  116.     {
  117.     case 'i':
  118.       ignore_environment = 1;
  119.       break;
  120.     case 'u':
  121.       break;
  122.     default:
  123.       usage ();
  124.     }
  125.     }
  126.  
  127.   if (optind != argc && !strcmp (argv[optind], "-"))
  128.     ignore_environment = 1;
  129.   
  130.   environ = dummy_environ;
  131.   environ[0] = NULL;
  132.  
  133.   if (!ignore_environment)
  134.     for (; *envp; envp++)
  135.       putenv (*envp);
  136.  
  137.   optind = 0;            /* Force GNU getopt to re-initialize. */
  138.   while ((optc = getopt_long (argc, argv, "+iu:", longopts, (int *) 0)) != EOF)
  139.     if (optc == 'u')
  140.       putenv (optarg);        /* Requires GNU putenv. */
  141.  
  142.   if (optind != argc && !strcmp (argv[optind], "-"))
  143.     ++optind;
  144.  
  145.   while (optind < argc && index (argv[optind], '='))
  146.     putenv (argv[optind++]);
  147.  
  148.   /* If no program is specified, print the environment and exit. */
  149.   if (optind == argc)
  150.     {
  151.       while (*environ)
  152.     puts (*environ++);
  153.       exit (0);
  154.     }
  155.  
  156.   execvp (argv[optind], &argv[optind]);
  157.   error (errno == ENOENT ? 127 : 126, errno, "%s", argv[optind]);
  158. }
  159.  
  160. void
  161. usage ()
  162. {
  163.   fprintf (stderr, "\
  164. Usage: %s [-] [-i] [-u name] [--ignore-environment] [--unset=name]\n\
  165.        [name=value]... [command [args...]]\n",
  166.        program_name);
  167.   exit (2);
  168. }
  169.